#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct ListNode {
    int val;
    struct ListNode* next;
};

void Print(struct ListNode* head)
{
    while (head)
    {
        printf("%d->", head->val);
        head = head->next;
    }
    printf("NULL\n");
}

struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* ret = head;
    if (head == NULL)
    {
        return head;
    }
    while (head->val == val)
    {
        head = head->next;
        free(ret);
        ret = head;
    }
    struct ListNode* p = ret;
    while (ret)
    {
        ret = ret->next;
        if (ret->val == val)
        {
            p->next = ret->next;
            free(ret);
        }
        p = p->next;
        ret = p;
    }
    return head;
}


struct ListNode* middleNode(struct ListNode* head) {
    struct ListNode* p1 = head;
    struct ListNode* p2 = head;
    while (p1 && p1->next)
    {
        p1 = p1->next;
        p2 = p2->next;
        p1 = p1->next;
    }
    return p2;
}

struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode* p1 = head;
    struct ListNode* p2 = NULL;
    struct ListNode* p3 = head;
    while (p1)
    {
        p3 = p1->next;
        p1->next = p2;
        p2 = p1;
        p1 = p3;
    }
    return p2;
}

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k) {
    // write code here
    struct ListNode* p1 = pListHead;
    while (k--)
    {
        if (p1 == NULL)
        {
            return NULL;
        }
        p1 = p1->next;
    }
    struct ListNode* p2 = pListHead;
    while (p1)
    {
        p1 = p1->next;
        p2 = p2->next;
    }
    return p2;
}

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if (list1 == NULL )
    {
        return list2;
    }
    if (list2 == NULL )
    {
        return list1;
    }
    struct ListNode* p = NULL;
    struct ListNode* ph = NULL;
    while (list1 && list2)
    {
        if (p == NULL)
        {
            p = list1->val > list2->val ? list2 : list1;
            ph = p;
        }
        else
        {
            p->next = list1->val > list2->val ? list2 : list1;
            p = p->next;
        }
        if (list1->val > list2->val)
        {
            list2 = list2->next;
        }
        else
        {
            list1 = list1->next;
        }
    }
    if (list1)
    {
        p->next = list1;
    }
    if (list2)
    {
        p->next = list2;
    }
    return ph;
}

class PalindromeList {
public:
    bool chkPalindrome(ListNode* A) {
        // write code here
        if (A == NULL)
        {
            return false;
        }
        ListNode* p1 = A;
        ListNode* p2 = A;
        while (p1 && p1->next)
        {
            p1 = p1->next;
            p2 = p2->next;
            p1 = p1->next;
        }
        ListNode* p3 = NULL;
        while (p2)
        {
            p1 = p2->next;
            p2->next = p3;
            p3 = p2;
            p2 = p1;
        }
        while (p3 && A)
        {
            if (A->val == p3->val)
            {
                p3 = p3->next;
                A = A->next;
            }
            else
            {
                return false;
            }
        }
        return true;
    }
};

int main()
{
    ListNode* p1 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* p2 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* p3 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* p4 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* p5 = (ListNode*)malloc(sizeof(ListNode));
    ListNode* p6 = (ListNode*)malloc(sizeof(ListNode));
    p1->next = p2;
    p2->next = p3;
    p3->next = NULL;
    p4->next = p5;
    p5->next = p6;
    p6->next = NULL;
    p1->val = 1;
    p2->val = 2;
    p3->val = 6;
    p4->val = 3;
    p5->val = 4;
    p6->val = 6;
    ListNode* p = mergeTwoLists(p1, p4);
    Print(p);
    return 0;
}